package com.fable.sp.core.service.impl;

import com.fable.sp.core.service.GlobalPreferencesProvider;
import com.fable.sp.core.service.ValidationConfig;
import com.fable.sp.core.widget.Preference;
import com.fable.sp.core.widget.Widget;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.InitializingBean;

import java.util.List;

/**
 * .
 *
 * @author stormning on 16/9/14.
 */
public abstract class AbstractValidationPreferencesProvider implements GlobalPreferencesProvider, InitializingBean {

    private List<Preference> preferences = Lists.newArrayList();

    @Override
    public boolean support(Widget widget) {
        if (widget instanceof ValidationConfig) {
            return ((ValidationConfig) widget).useDefaultValidation();
        }
        return false;
    }

    @Override
    public List<Preference> getPreferences() {
        return preferences;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        setupPreferences(getPreferences());
    }

    protected abstract void setupPreferences(List<Preference> preferences);
}
